package com.atguigu.tree;

public class BinaryTreeDemo {
    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();

        HeroNode rootNode = new HeroNode(1, "宋江");
        HeroNode node2 = new HeroNode(2, "吴用");
        HeroNode node3 = new HeroNode(3, "卢俊义");
        HeroNode node4 = new HeroNode(4, "林冲");
        HeroNode node5 = new HeroNode(5, "关胜");
        HeroNode node6 = new HeroNode(6, "秦明");



        binaryTree.setRoot(rootNode);
        rootNode.setLeft(node2);
        rootNode.setRight(node3);
        node2.setRight(node6);
        node3.setLeft(node5);
        node3.setRight(node4);
        /**
         * 1
         * L2       R3
         *  R6      L5   R4
         *
         *
         */


        // 前序遍历->1,2,6,3,5,4
//        System.out.println("前序遍历");
//        binaryTree.preOrder();
//
//        // 中序遍历->2,6,1,5,3,4
//        System.out.println("中序遍历");
//        binaryTree.midOrder();
//
//        // 后序遍历->6,2,5,4,3,1
//        System.out.println("后序遍历");
//        binaryTree.postOrder();

        // 前序查找
//        HeroNode heroNode = binaryTree.preOrderFind(5);
//        System.out.println(heroNode);

        // 中序查找
//        HeroNode heroNode = binaryTree.midOrderFind(5);
//        System.out.println(heroNode);

        // 后序查找
//        HeroNode heroNode = binaryTree.postOrderFind(5);
//        System.out.println(heroNode);

        // 删除
        binaryTree.preOrder();
        boolean remove = binaryTree.remove(3);
        System.out.println("删除:"+remove);
        binaryTree.preOrder();

    }
}

class BinaryTree{
    private HeroNode root;



    public void setRoot(HeroNode root) {
        this.root = root;
    }

    public void preOrder(){
        if (root!=null) {
            root.preOrder();
        }else{
            System.out.println("数据为空");
        }
    }

    public void midOrder(){

        if (root!=null) {
            root.midOrder();
        }else{
            System.out.println("数据为空");
        }
    }

    public void postOrder(){

        if (root!=null) {
            root.postOrder();
        }else{
            System.out.println("数据为空");
        }
    }

    public HeroNode preOrderFind(int id){
        if(root!=null){
            return root.preOrderFind(id);
        }else{
            return null;
        }
    }

    public HeroNode midOrderFind(int id){
        if(root!=null){
            return root.midOrderFind(id);
        }else{
            return null;
        }
    }

    public HeroNode postOrderFind(int id){
        if(root!=null){
            return root.postOrderFind(id);
        }else{
            return null;
        }
    }

    public boolean remove(int id){
        if(this.root!=null){
            if(this.root.getId()==id){
                this.root=null;
                return true;
            }else{
                return this.root.remove(id);
            }
        }else{
            return false;
        }
    }
}


class HeroNode{
    private Integer id;
    private String name;

    private HeroNode left;
    private HeroNode right;

    public HeroNode(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    public void preOrder(){
        System.out.println(this);
        if (left!=null) {
            left.preOrder();
        }
        if (right!=null) {
            right.preOrder();
        }
    }

    public void midOrder(){

        if (left!=null) {
            left.midOrder();
        }
        System.out.println(this);
        if (right!=null) {
            right.midOrder();
        }
    }

    public void postOrder(){

        if (left!=null) {
            left.postOrder();
        }
        if (right!=null) {
            right.postOrder();
        }
        System.out.println(this);
    }

    public HeroNode preOrderFind(int id){
        System.out.println("id比较:"+this.id);
        if (this.id==id) {
            return this;
        }
        if (this.left!=null) {
            HeroNode heroNode = this.left.preOrderFind(id);
            if (heroNode!=null) {
                return heroNode;
            }
        }
        if(this.right!=null){
            HeroNode heroNode = this.right.preOrderFind(id);
            if (heroNode!=null) {
                return heroNode;
            }
        }
        return null;

    }

    public HeroNode midOrderFind(int id){

        if (this.left!=null) {
            HeroNode heroNode = this.left.midOrderFind(id);
            if (heroNode!=null) {
                return heroNode;
            }
        }
        System.out.println("id比较:"+this.id);
        if (this.id==id) {
            return this;
        }
        if(this.right!=null){
            HeroNode heroNode = this.right.midOrderFind(id);
            if (heroNode!=null) {
                return heroNode;
            }
        }
        return null;
    }

    public HeroNode postOrderFind(int id){

        if (this.left!=null) {
            HeroNode heroNode = this.left.postOrderFind(id);
            if (heroNode!=null) {
                return heroNode;
            }
        }
        if(this.right!=null){
            HeroNode heroNode = this.right.postOrderFind(id);
            if (heroNode!=null) {
                return heroNode;
            }
        }
        System.out.println("id比较:"+this.id);
        if (this.id==id) {
            return this;
        }

        return null;
    }

    public boolean remove(int id){
        System.out.println("id比较:"+this.id);
        if (this.left!=null) {
            if(this.left.id==id){
                this.left=null;
                return true;
            }
            boolean b = this.left.remove(id);
            if (b) {
                return b;
            }
        }
        if(this.right!=null){
            if (this.right.id==id) {
                this.right=null;
                return true;
            }
            boolean b = this.right.remove(id);
            if(b){
                return b;
            }
        }
        return false;

    }

}